﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Reflection;

namespace Wys.DataForm.CSharp.PR
{
    public partial class EnglishC : Wys.Control.Vb.AutoAdjustFrom
    {
        public EnglishC()
        {
            InitializeComponent();
        }

        private void EnglishC_Load(object sender, EventArgs e)
        {
            ADODB.Recordset rs = ct.GetTable("select XSCWebSiteCj.WebSiteUrl,XscBugType.Name,XSCWebSiteCj.ContentRegex,XSCWebSiteCj.type from XSCWebSiteCj inner join XscBugType on XSCWebSiteCj.type=XscBugType.id");
            if (!rs.EOF)
            {
                while (!rs.EOF)
                {
                    ListViewItem it = new ListViewItem();
                    it.Text = rs.Fields["Name"].Value.ToString();
                    it.SubItems.Add(rs.Fields["WebSiteUrl"].Value.ToString());
                    it.SubItems.Add(rs.Fields["ContentRegex"].Value.ToString());
                    it.SubItems.Add(rs.Fields["type"].Value.ToString());
                    myListView1.Items.Add(it);
                    rs.MoveNext();
                }
                rs.Close();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                rs = null;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            //删除所有数据      
           
            foreach (ListViewItem lt in myListView1.Items)
            {
                if (lt.Checked)
                {
                    DataTable dt = UTIL.CRss.ReadRss(lt.SubItems[1].Text);
                    foreach(DataRow dr in dt.Rows)
                    {
                        try
                        {
                            ADODB.Recordset rs = ct.GetUpdateRs("select * from XSCEnglishWz where title=''");
                            HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(dr["Link"].ToString());
                            lblStatus.Text = dr["Link"].ToString();
                            HttpWebResponse hwp = hwr.GetResponse() as HttpWebResponse;
                            hwr.Method = "POST";
                            using (System.IO.StreamReader sr = new StreamReader(hwp.GetResponseStream()))
                            {
                                String strContent = sr.ReadToEnd();
                                if (strContent != "")
                                {
                                    Match mc = Regex.Match(strContent, lt.SubItems[2].Text);
                                    if (mc.Groups.Count > 0)
                                    {
                                        rs.AddNew(System.Reflection.Missing.Value , System.Reflection.Missing.Value);
                                        rs.Fields["Title"].Value = dr["Title"];
                                        rs.Fields["OptUser"].Value = this.strName;
                                        rs.Fields["type"].Value = lt.SubItems[3].Text;
                                        rs.Fields["OPTIME"].Value = DateTime.Now.ToString() ;
                                        rs.Fields["AddTime"].Value = DateTime.Now.ToString();
                                        rs.Fields["OptUser"].Value = this.strName;
                                        rs.Update(System.Reflection.Missing.Value, System.Reflection.Missing.Value);
                                        rs.Close();
                                        System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                                        rs = null;
                                        int  strMaxId = ct.GetMaxIdInsertForm("XSCEnglishWz");
                                        rs = ct.GetUpdateRs("select * from XSCEnglishWz where Id='" + strMaxId.ToString()+"'");
                                        rs.Fields["Content"].Value = mc.Groups[1].Value;
                                        rs.Update(System.Reflection.Missing.Value, System.Reflection.Missing.Value);
                                        rs.Close();
                                        System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                                        rs = null;
                                        lblStatus.Text=dr["Title"].ToString();
                                    }
                                }
                            }
                        }
                        catch (Exception me)
                        {
                            UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());

                        }                     

                    }                    
                    
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

        }

      


    }
}
